package com.jokul.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.jokul.connection.DataBase;
import com.jokul.model.User;

/**
 * @author: jokul
 * @2013-2-26 上午10:35:34
 */

public class UserDaoImpl implements UserDao {
	
	private static final Logger LOGGER = Logger.getLogger(UserDaoImpl.class);

	public Integer insert(User user) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;

		try {
			connection = DataBase.getConnection();
			String sql = "insert into user (user_name,password,email,last_ip,last_time,is_login) values (?,?,?,?,?,?)";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, user.getUserName());
			preparedStatement.setString(2, user.getPassword());
			preparedStatement.setString(3, user.getEmail());
			preparedStatement.setString(4, user.getLastIP());
			preparedStatement.setTimestamp(5, new Timestamp(new Date().getTime()));
			preparedStatement.setInt(6, 0);
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			LOGGER.error("添加用户失败！", new Throwable(e.getMessage()));
		} finally {
			try {
				if(preparedStatement != null) {
					preparedStatement.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				if(connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return 0;
	}

	public List<User> getList() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		List<User> list = new ArrayList<User>();
		try {
			connection = DataBase.getConnection();
			String sql = "select * from user";
			preparedStatement = connection.prepareStatement(sql);
			ResultSet resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				User user = new User();
				user.setUserId(resultSet.getInt(1));
				user.setUserName(resultSet.getString(2));
				user.setPassword(resultSet.getString(3));
				user.setEmail(resultSet.getString(4));
				user.setLastIP(resultSet.getString(5));
				user.setLastTime(resultSet.getTimestamp(6));
				user.setIsLogin(resultSet.getInt(7));				
				list.add(user);
			}
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if(preparedStatement != null) {
					preparedStatement.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				if(connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	@Override
	public User getUser(String userName) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		String sql = "select password from user where user_name = ?";
		User user = new User();
		try {
			connection = DataBase.getConnection();
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, userName);
			ResultSet resultSet = preparedStatement.executeQuery();
			while(resultSet.next()) {
				user.setPassword(resultSet.getString(1));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if(preparedStatement != null) {
					preparedStatement.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				if(connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		return user;
	}
}
